package com.liujun.spring4.demo.aop.application.i18n.base;

import javafx.scene.input.DataFormat;
import org.springframework.cglib.core.Local;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 本地化工具类
 *
 * @author liujun
 * @version 0.0.1
 * @date 2018/09/10
 */
public class LocalFormat {

  public static void main(String[] args) {

    numberFormat();

    dataFormat();

      messageFormat();
  }

  /** 数字的国际化显示 */
  public static void numberFormat() {
    Locale local = Locale.US;
    NumberFormat curFormat = NumberFormat.getCurrencyInstance(local);
    double amt = 12345.78;
    System.out.println(curFormat.format(amt));
  }

  public static void dataFormat() {
    Locale local = Locale.US;
    Date datetime = new Date();
    DateFormat df = null;
    df = DateFormat.getDateInstance(DateFormat.FULL, local);

    System.out.println(((DateFormat) df).format(datetime));
  }

  public static void messageFormat() {
    // 格式化信息串
    String pattern1 = "{0},你好，你于{1}在工商银行存入{2}元";
    String parrern2 = "At {1,time,short} ON {1,date,long} , {0} paid {2,number,currency}.";

    // 2,用于动态替换占位符参数
    Object[] params = {"John", new GregorianCalendar().getTime(), 1.0E3};

    // 3,使用默认的本地化对象格式信息
    String msg1 = MessageFormat.format(pattern1, params);

    // 4,使用指定的本地化对象格式化信息
    MessageFormat mf = new MessageFormat(parrern2, Locale.US);
    String msg2 = mf.format(params);
    System.out.println(msg1);
    System.out.println(msg2);
  }
}
